package cn.engage.org.controller;

import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import cn.engage.org.domain.Employee;
import cn.engage.org.query.EmployeeQuery;
import cn.engage.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@CrossOrigin
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }
    // 分页查询
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }
    // 新增和修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        if (null == employee.getId()){
            // 添加
            employeeService.add(employee);
        }else {
            // 修改
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }
    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }
    // 批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        employeeService.patchDelete(ids);

        return AjaxResult.me().success();
    }
    // 查询单个get请求 localhost:8080/dept/1
    @GetMapping("/{id}")
    public List<Employee> findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

}
